package edu.pw.treegrid.client.report.view;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.widgets.ImgProperties;
import com.smartgwt.client.widgets.TransferImgButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DragStopEvent;
import com.smartgwt.client.widgets.events.DragStopHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VStack;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;

import edu.pw.treegrid.client.report.controller.ReportController;
import edu.pw.treegrid.client.report.model.ReportColumnDescriptionClient;
import edu.pw.treegrid.client.report.model.ReportHierarchy;
import edu.pw.treegrid.shared.ReportColumnDescription;


public class HierarchyPanel extends HStack {
	private HierarchyList otherColumnList;
	private HierarchyList hierarchyColumnList;
	private ReportController controller;
	private List<ReportColumnDescriptionClient> candidateColumns;
	
	public RecordList getHierarchy() {
		return hierarchyColumnList.getDataAsRecordList();
	}
	
	public HierarchyPanel(List<ReportColumnDescriptionClient> columns, ReportController controller, Collection<ReportHierarchy> reportHierarchies) {
		//super();
		this.controller = controller;
		this.setWidth100();
		
		this.candidateColumns = this.getCandidateColumns(columns);
		
		otherColumnList = new HierarchyList();
        otherColumnList.setCanDragRecordsOut(true);
        otherColumnList.setCanAcceptDroppedRecords(true);
        otherColumnList.setCanReorderFields(true);
        otherColumnList.setDragDataAction(DragDataAction.MOVE);
        otherColumnList.setData(getCandidateColumns(columns).toArray(new ReportColumnDescriptionClient[0]));
        otherColumnList.addDragStopHandler(new DragStopHandler() {
			@Override
			public void onDragStop(DragStopEvent event) {
				HierarchyPanel.this.controller.onHierarchyChanged();
			}
		});
        

        hierarchyColumnList = new HierarchyList();
        hierarchyColumnList.setCanDragRecordsOut(true);
        hierarchyColumnList.setCanAcceptDroppedRecords(true);
        hierarchyColumnList.setCanReorderRecords(true);
        hierarchyColumnList.addDragStopHandler(new DragStopHandler() {
			@Override
			public void onDragStop(DragStopEvent event) {
				HierarchyPanel.this.controller.onHierarchyChanged();
			}
		});
        
        VStack vStack = new VStack(10);
        vStack.setWidth(32);
        vStack.setHeight(74);
        vStack.setLayoutAlign(Alignment.CENTER);

        TransferImgButton rightImg = new TransferImgButton(TransferImgButton.RIGHT);
        rightImg.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                hierarchyColumnList.transferSelectedData(otherColumnList);
                HierarchyPanel.this.controller.onHierarchyChanged();
            }
        });
        vStack.addMember(rightImg);

        TransferImgButton leftImg = new TransferImgButton(TransferImgButton.LEFT);
        leftImg.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                otherColumnList.transferSelectedData(hierarchyColumnList);
                HierarchyPanel.this.controller.onHierarchyChanged();
            }
        });
        vStack.addMember(leftImg);

        VStack buttonsStack = new VStack();
        buttonsStack.addMember(new Label("Gotowe hierarchie:"));
        for(final ReportHierarchy h: reportHierarchies) {
                Button b = new Button();
                b.setWidth100();
                b.setAlign(Alignment.LEFT);
                b.setTitle(h.getLabel());
                b.addClickHandler(new ClickHandler() {
                                @Override
                                public void onClick(ClickEvent event) {
                                        HierarchyPanel.this.controller.onReportHierarchyChanged(h);
                                }
                        });
                buttonsStack.addMember(b);
        }
        buttonsStack.setWidth("30%");
        
        VStack otherColumnStack = new VStack();
        otherColumnStack.addMember(new Label("Kolumny do wyboru:"));
        otherColumnList.setWidth100();
        otherColumnList.setHeight("80%");
        otherColumnStack.addMember(otherColumnList);
        otherColumnStack.setWidth("30%");
        
        VStack hierarchyColumnStack = new VStack();
        hierarchyColumnStack.addMember(new Label("Kolumny w hierarchii:"));
        hierarchyColumnList.setWidth100();
        hierarchyColumnList.setHeight("80%");
        hierarchyColumnStack.addMember(hierarchyColumnList);
        hierarchyColumnStack.setWidth("30%");
        
        this.addMember(buttonsStack);
        this.addMember(otherColumnStack);
        this.addMember(vStack);
        this.addMember(hierarchyColumnStack);
	}
	
	private List<ReportColumnDescriptionClient> getCandidateColumns(List<ReportColumnDescriptionClient> columns) {
		List<ReportColumnDescriptionClient> result = new LinkedList<ReportColumnDescriptionClient>();
		
		for(ReportColumnDescriptionClient c : columns) {
			if(c.getCategory().isCandidate()) {
				result.add(c);
			}
		}
		
		return result;
	}
	
	public boolean isValidHierarchy() {
		return !this.hierarchyColumnList.getDataAsRecordList().isEmpty();
	}
	
	class HierarchyList extends ListGrid {

		HierarchyList() {
	        setWidth("33%");
	        setCellHeight(24);
	        setImageSize(16);
	        setShowEdges(true);
	        setBorder("0px");
	        setBodyStyleName("normal");
	        setShowHeader(false);
	        setLeaveScrollbarGap(false);
	        setEmptyMessage("<br><br>Drag &amp; drop parts here");

	        ListGridField partNameField = new ListGridField(ReportColumnDescription.TITLE);
	        partNameField.setAlign(Alignment.LEFT);
	        partNameField.setWidth("90%");
	        ListGridField img = new ListGridField(ReportColumnDescription.IMAGE_SRC);
	        img.setType(ListGridFieldType.IMAGE);
	        img.setWidth("10%");
	        setFields(img, partNameField);

	        setTrackerImage(new ImgProperties("pieces/24/cubes_all.png", 24, 24));
	    }
	}
	

    public void setReportHierarchy(ReportHierarchy h) {
            List<ReportColumnDescriptionClient> hierarchyColumns = new LinkedList<ReportColumnDescriptionClient>();
            List<ReportColumnDescriptionClient> otherColumns = new LinkedList<ReportColumnDescriptionClient>();
            
            hierarchyColumns.addAll(h.getColumns());
            
            for(ReportColumnDescriptionClient rc: this.candidateColumns) {
                    if(!h.getColumns().contains(rc)) {
                            otherColumns.add(rc);
                    }
            }
            
            this.hierarchyColumnList.setData(hierarchyColumns.toArray(new ReportColumnDescriptionClient[0]));
            this.otherColumnList.setData(otherColumns.toArray(new ReportColumnDescriptionClient[0]));
    }
	
}
